package com.wechat.app.ui.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.wechat.app.ui.screens.chat.ChatDetailScreen
import com.wechat.app.ui.screens.chat.HomeScreen
import com.wechat.app.ui.screens.contacts.ContactsScreen
import com.wechat.app.ui.screens.contacts.ProfileScreen
import com.wechat.app.ui.screens.discover.DiscoverScreen
import com.wechat.app.ui.screens.me.MeScreen

object NavRoutes {
    const val HOME = "home"
    const val CONTACTS = "contacts"
    const val DISCOVER = "discover"
    const val ME = "me"
    const val CHAT_DETAIL = "chat_detail/{chatName}?chatAvatar={chatAvatar}"
    const val PROFILE = "profile/{contactName}?contactAvatar={contactAvatar}"
    const val MOMENTS = "moments"
}

var navController: NavHostController? = null

fun navigateToChatDetail(chatName: String, chatAvatar: String) {
    val controller = navController
    if (controller != null) {
        try {
            // URL 编码参数
            val encodedName = java.net.URLEncoder.encode(chatName, "UTF-8")
            val encodedAvatar = if (chatAvatar.isNotEmpty()) {
                java.net.URLEncoder.encode(chatAvatar, "UTF-8")
            } else {
                ""
            }
            // 使用查询参数方式，避免空字符串导致的路由匹配问题
            val route = if (encodedAvatar.isNotEmpty()) {
                "chat_detail/$encodedName?chatAvatar=$encodedAvatar"
            } else {
                "chat_detail/$encodedName"
            }
            controller.navigate(route) {
                // 防止重复导航
                launchSingleTop = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

fun navigateToProfile(contactName: String, contactAvatar: String?) {
    val controller = navController
    if (controller != null) {
        try {
            // URL 编码参数
            val encodedName = java.net.URLEncoder.encode(contactName, "UTF-8")
            val encodedAvatar = if (contactAvatar != null && contactAvatar.isNotEmpty()) {
                java.net.URLEncoder.encode(contactAvatar, "UTF-8")
            } else {
                ""
            }
            // 使用查询参数方式，避免空字符串导致的路由匹配问题
            val route = if (encodedAvatar.isNotEmpty()) {
                "profile/$encodedName?contactAvatar=$encodedAvatar"
            } else {
                "profile/$encodedName"
            }
            controller.navigate(route) {
                // 防止重复导航
                launchSingleTop = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

fun navigateToMoments() {
    val controller = navController
    if (controller != null) {
        try {
            controller.navigate(NavRoutes.MOMENTS) {
                launchSingleTop = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

fun navigateBack() {
    val controller = navController
    if (controller != null) {
        try {
            if (controller.previousBackStackEntry != null) {
                controller.popBackStack()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

